from users.models import User
from aijia_project.utils.Error_statuscode import ErrorCode


def generate_house_detail(request, data):
    """生成房源详情API需要的返回结果的辅助函数"""
    # 重命名字段
    for i in data['comments']:
        i['user_name'] = i.pop('user')
        i['ctime'] = i.pop('create_time')
    data['facilities'] = data.pop('facility')

    # 添加新字段
    user = User.objects.get(id=data['user_id'])
    data['user_avata'] = user.avatar.url  # 用户头像，是ImageField字段
    data['user_name'] = user.real_name  # 用户真实姓名

    # 构成接口文档要求的返回结果
    result = {
        'data': {
            'house': data,
            'user_id': request.user.id
        },
        "errmsg": "OK",
        "errno": ErrorCode.OK
    }

    return result
